package com.sgcc.sx.pmos.mobileapp.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.mobileapp.utils.SmUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 新增户号-----国家电网弹窗
 */
public class AccountCountryPopup extends CenterPopupView {
    private LinearLayout linner_type;
    private TextView tv_cancel, tv_confirm;
    private EditText edit_huhao, edit_pass;
    Map<String, Object> params = new HashMap<>();
    boolean isClick;

    public AccountCountryPopup(@NonNull Context context, boolean isClick, OnClickListener onClickListener) {
        super(context);
        this.isClick = isClick;
        this.onClickListener = onClickListener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.account_country_pop;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        String[] strings = new String[3];
        strings[0] = "国家电网";
        strings[1] = "地方电网";
        strings[2] = "增量配电网";
        edit_huhao = findViewById(R.id.edit_huhao);
        edit_pass = findViewById(R.id.edit_pass);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(edit_huhao.getText().toString())) {
                    if (!TextUtils.isEmpty(edit_pass.getText().toString())) {
                        params.put("powerGridType", "1");
                        params.put("consNo", edit_huhao.getText().toString());
                        String securt = SmUtils.encryptSM2(edit_pass.getText().toString(),
                                "043d80860946979f721ea4414bff078c11d14998d5d8edf39c678b6b04d006d18e740f97e6257f7bd7df631b564a55ccb53fd31e0237474e6f162ff30972380d5d");
                        params.put("passwd", securt);
                        if (onClickListener != null) {
                            onClickListener.onPositiveClick(AccountCountryPopup.this, params);
                        }
                    } else {
                        ToastUtils.showShort("请填写完整");
                    }
                } else {
                    ToastUtils.showShort("请填写完整");
                }

            }
        });
        linner_type = findViewById(R.id.linner_type);
        if (isClick)
            linner_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new XPopup.Builder(getContext())
                            .hasShadowBg(false)
                            .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                            .asAttachList(strings,
                                    null,
                                    new OnSelectListener() {
                                        @Override
                                        public void onSelect(int position, String text) {
                                            if (onClickListener != null) {
                                                onClickListener.onTypeClick(AccountCountryPopup.this, position);
                                            }

                                        }
                                    }, 0, 0, Gravity.CENTER).show();
                }
            });
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

//    @Override
//    protected int getMaxHeight() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
//    }

    @Override
    protected boolean onBackPressed() {
        return true;
    }


    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickListener onClickListener;


    public interface OnClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(AccountCountryPopup popup, Map<String, Object> params);

        /**
         * 点击确定按钮事件
         */
        public void onTypeClick(AccountCountryPopup popup, int position);

    }
}
